#include "ModelRenderComp.h"
#include <SEngine/Render/Entity/Loader/ModelLoader.h>
#include <SEDebug/Log/Log.h>
#include <SEngine/Render/Entity/BoundingBox.h>
#include <SECore/Ecs/Actor.h>
#include <SECore/Manager.h>
#include <SERender/Resourses/Shader.h>

namespace SEngine
{
    void ModelRenderComp::Attach()
    {
        m_actor.Subscribe<ModelRenderComp, glm::mat4&>("SignalUpdateTransform", [this](glm::mat4 & model) {
            m_model.Transfrom(model);
            return SlotState::Keep;
        });
    }

    void ModelRenderComp::Load(const std::string &modelName)
    {
        try {
            m_model.SetMeshFilter(Manager::GetInstance().Get<MeshFilter>(modelName).value(), Manager::GetInstance().Get<Material>("Standard").value());
            Log::GetInstance().Info("Load Model {} success", modelName);
        } catch(const std::exception& e) {
            Log::GetInstance().Error("Model Load: {}", e.what());
        }
    }

    void ModelRenderComp::OnModelSerialize(std::string &str)
    {
        // str = m_modelAssets.data ? m_modelAssets.data->Serialize() : "";
    }

    void ModelRenderComp::OnModelDeserialize(const std::string &str)
    {
        // if (str == "~") {
        //     return;
        // }
        // if (!m_modelAssets.data) {
        //     m_modelAssets.data = std::make_unique<Model>();
        // }
        // m_modelAssets.data->Deserialize(str);
    }

} // namespace SEngine
